package com.g2room.myuall.tools;

import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.g2room.myuall.grap.ky.model.po.PeeUnivInfo;
import com.g2room.myuall.util.DbUtil;

public class DownloadUnivImgs implements Runnable {
	private List<PeeUnivInfo> univs = null;
	private static String dir = "D:/userdata/imags/univ/img/";
	@Override
	public void run() {
		// TODO Auto-generated method stub
		dl();
	}
	public static void main(String[] args) {
		// 865 / 3 = 289
		List<PeeUnivInfo> univs = getImages();
		

		DownloadUnivImgs dui = new DownloadUnivImgs();
		dui.univs = univs;//.subList(i, Math.min(i + l, univs.size()));
		dui.dl();
//		i = i+l;
//		System.out.println(i);
//		Thread t = new Thread(dui);
//		t.start();
		
		
//		int l = 289;
//		for(int i = 0; i<8651;) {
//			DownloadUnivImgs dui = new DownloadUnivImgs();
//			dui.univs = univs.subList(i, Math.min(i + l, univs.size()));
//			i = i+l;
//			System.out.println(i);
//			Thread t = new Thread(dui);
//			t.start();
//		}
		
	}
	
	public void dl() {
		while(null != univs && univs.size() > 0) {
			for(PeeUnivInfo ui : univs) {
				System.out.print(ui.getId()+",     ");
				try {
					if(StringUtils.isNotBlank(ui.getLogo()))
					ImageUtil.downloadImage2Ky(ui.getLogo(), "img/", ui.getId());
				} catch (Exception e) {
					System.out.println(ui.getLogo());
					e.printStackTrace();
				}
			}
		}
	//				if(StringUtils.isNotBlank(ui.getLogo()))
	//				logoPath = ImageUtil.downloadImage2Ky(ui.getLogo(), "imgs");
	//			} catch (Exception e) {
	//				if(!e.toString().contains("FileNotFoundException")) {
	//					e.printStackTrace();
	//				}
	////			} finally {
	////				updateImg(logoPath, ui.getId());
	//			}
	//			try {
	//
	//				if(StringUtils.isNotBlank(ui.getBadge()))
	//				badgePath = ImageUtil.downloadImage2Ky(ui.getBadge(), "badge");
	//			} catch (Exception e) {
	//				if(!e.toString().contains("FileNotFoundException")) {
	//					e.printStackTrace();
	//				}
	//			}
//				updateImg(logoPath, ui.getId());
//			}
//			univs = getImages();
//			try {
//				Thread.sleep(200);
//			} catch (InterruptedException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}
	}

	public  static List<PeeUnivInfo> getImages() {
		List<PeeUnivInfo> univs = new ArrayList<PeeUnivInfo>();
		try {
			Connection connection = DbUtil.getConn("myuall_ol");
			Statement statement = connection.createStatement();
			String sql = "select id,url from univ_beauty_imgs";
			ResultSet rs = statement.executeQuery(sql);
			File[] fs = new File(dir).listFiles();
			List<Integer> ids = new ArrayList<Integer>();
			for(File f : fs) {
				String fn = f.getName().substring(0, f.getName().indexOf("."));
				ids.add(Integer.parseInt(fn));
			}
			while(rs.next()) {
				if(ids.contains(rs.getInt("id"))) continue;
				PeeUnivInfo univ = new PeeUnivInfo();
				univ.setId(rs.getInt("id"));
				univ.setLogo(rs.getString("url"));
				univs.add(univ);
			}
			rs.close();
			statement.close();
		} catch(Exception e) {
			e.printStackTrace();
		}
		return univs;
	}
	

	public static void updateImg(String imgUrl, int id) {
		PreparedStatement grapPs = null;
		try {
			grapPs = DbUtil.getConn().prepareStatement("update univ_info set badge1=? where id=?");
			grapPs.setString(1, imgUrl);
//			grapPs.setString(2, url);
			grapPs.setInt(2, id);
			grapPs.execute();
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(null != grapPs) {
				try {
					grapPs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
}
